package com.ogawa.chair7808.widget;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.ogawa.base.Constant.CommmandNum;
import com.easepal.socketiolib.model.SocketModel;
import com.ogawa.base.utils.CommonUtil;
import com.ogawa.chair7808.R;
import com.ogawa.chair7808.manager.DataManager;
import com.ogawa.chair7808.observer.SocketDataObserver;
import com.ogawa.chair7808.ui.activity.BaseActivity;
import com.ogawa.chair7808.utils.Activity7808Utils;
import com.example.reslib.utils.DisplayUtil;
import com.example.reslib.utils.ToastUtils;


/**
 * 普通的头部布局
 * Created by FHB on 2018/3/29.
 */

public class LayoutNormalTitle7506 extends RelativeLayout implements View.OnClickListener, SocketDataObserver.SocketReceive {
    private ImageView mIvBack;
    private ImageView mIvWifi;
    private ImageView mIvPower;
    private TextView mTvTitle;
    private BaseActivity mActivity;
    private boolean isPowerOn;
    public LayoutNormalTitle7506(Context context) {
        super(context);
        mActivity = (BaseActivity) context;
    }

    public LayoutNormalTitle7506(Context context, AttributeSet attrs) {
        super(context, attrs);
        mActivity = (BaseActivity) context;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mIvWifi = findViewById(R.id.title_wifi);
        mIvBack = findViewById(R.id.title_back);
        mIvPower = findViewById(R.id.title_power_close);
        mTvTitle = findViewById(R.id.title_name);
        mIvBack.setOnClickListener(this);
//        mIvWifi.setOnClickListener(this);
        mIvPower.setOnClickListener(this);
        isPowerOn = DataManager.getInst().isOpen();
        mIvPower.setSelected(isPowerOn);

        this.setPadding(0, CommonUtil.getStatusBarHeight(mActivity)+DisplayUtil.dip2px(mActivity,10), 0, 0);
        SocketDataObserver.getInst().registerObserver(this);

    }

    public void setTitle(String title) {
        mTvTitle.setText(title);
    }
    public void setTitle(String title,String color) {
        mTvTitle.setText(title);
        mTvTitle.setTextColor(Color.parseColor(color));
    }


    public void setGreenBackBotton() {
        mIvBack.setImageResource(R.drawable.auto_back_seletor);

    }

    public void showWifi() {
        mIvBack.setVisibility(GONE);
        mIvWifi.setVisibility(VISIBLE);
    }
    public void showConnectWifi(){
          mIvPower.setVisibility(GONE);
          mTvTitle.setVisibility(GONE);
    }
    public void showPowerView(boolean isshow){
        mIvPower.setVisibility(isshow?VISIBLE:GONE);
    }


    public void setTitleNameGone() {
        mTvTitle.setVisibility(GONE);
    }

    public void setBackGone() {
        mIvBack.setVisibility(GONE);
    }

    public void onDestory() {
        SocketDataObserver.getInst().removeObserver(this);

    }

    public void initData() {
        isPowerOn = DataManager.getInst().isOpen();
        mIvPower.setSelected(isPowerOn);

    }





    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.title_back) {

            if (!TextUtils.isEmpty(mActivity.getmName()) && mActivity.getmName().equals("ProMasDetailActivity7506")) {
                Log.e("TitleBack",mActivity.getmName());
                Activity7808Utils.startProfessionMassaActivity(mActivity);
            }
            mActivity.finish();

        } else if (i == R.id.title_power_close) {

                if(!TextUtils.isEmpty(DataManager.getInst().getmSn())){
                    if(isPowerOn){
                        DataManager.getInst().sendCommond(CommmandNum.CLOSE,"0");
                    }
                    else {
                        if(DataManager.getInst().isInitSucceed()){
                            if(!DataManager.getInst().getDevice7808StateBean().getResetSucccessFlag().equals("1")){
                                DataManager.getInst().sendCommond(CommmandNum.OPEN,"1");
                            }else {
                                ToastUtils.showShortToast(getContext(),"机芯复位中，请稍等...");
                            }
                        }
                        else {
                            ToastUtils.showShortToast(getContext(),"初始化失败，请重新配置网络");
                        }



                    }
                }else {
                    ToastUtils.showShortToast(getContext(),"请先配置网络");
                }





        } else if (i == R.id.title_wifi) {
//            ActivityUtils.startSoftApActivity(mActivity);
            //跳转
        }

    }




    @Override
    public void onRceiveSocket(SocketModel model) {
        if (model.getKey().equals(CommmandNum.mStatusRunning)) {
            Log.e("send_PowerState",model.getValue()+"");
            isPowerOn = !model.getValue().equals("0");
            mIvPower.setSelected(isPowerOn);
        }
    }
}
